import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Caret;


public class ProgressTracker extends JFrame{
	static public String version = "0.1"; 
	
	String text1 = 
"" +
"[100] plan\n"+
"[10:100] Search simulation and learn to use it.\n"+ 
"[70] Create domains\n"+ 
"[33] foraging\n"+ 
"[50:100] mission\n"+ 
"[50] coordination methods\n"+ 
"[33:100] noise\n"+ 
"[33:100] repel\n"+ 
"[34:100] aggression\n"+ 
"[33] formation\n"+ 
"[50:10] mission\n"+ 
"[50] coordination methods\n"+ 
"[25:0] ind. shift\n"+ 
"[25:0] soc. shift\n"+ 
"[25:0] dumb anchoring\n"+ 
"[25:0] collab. anchoring\n"+ 
"[34] corridor\n"+ 
"[50:0] mission\n"+ 
"[50] coordination methods\n"+ 
"[33:0] ...\n"+ 
"[33:0] ...\n"+ 
"[34:0] ...\n"+ 
"[10] write EI calculation\n"+ 
"[30:100] behaviors for calculation\n"+ 
"[70:0] calculation\n"+ 
"[10] create test environment\n"+ 
"[50:0] test setup\n"+ 
"[50:0] test analyzer\n"+ 
"";
	String text2 = 
"" +
"[100] plan [37:++++------]\n"+
"...[10:100] Search simulation and learn to use it. [++++++++++]\n"+
"...[70] Create domains [35:+++-------]\n"+
"......[33] foraging [100:++++++++++]\n"+
".........[50:100] mission [++++++++++]\n"+
".........[50] coordination methods [100:++++++++++]\n"+
"............[33:100] noise [++++++++++]\n"+
"............[33:100] repel [++++++++++]\n"+
"............[34:100] aggression [++++++++++]\n"+
"...[33] formation [5:+---------]\n"+
"......[50:10] mission [+---------]\n"+
"......[50] coordination methods [0:----------]\n"+
".........[25:0] ind. shift [----------]\n"+
".........[25:0] soc. shift [----------]\n"+
".........[25:0] dumb anchoring [----------]\n"+
".........[25:0] collab. anchoring [----------]\n"+
"...[34] corridor [0:----------]\n"+
"......[50:0] mission [----------]\n"+
"......[50] coordination methods [0:----------]\n"+
".........[33:0] ... [----------]\n"+
".........[33:0] ... [----------]\n"+
".........[34:0] ... [----------]\n"+
"...[10] write EI calculation [30:+++-------]\n"+
"......[30:100] behaviors for calculation [++++++++++]\n"+
"......[70:0] calculation [----------]\n"+
"...[10] create test environment [0:----------]\n"+
"......[50:0] test setup [----------]\n"+
"......[50:0] test analyzer [----------]\n"+
"";
	String text = 
"" +
"[100][37:++++------] plan \n"+
"...[10:100][++++++++++] Search simulation and learn to use it. \n"+
"...[70][35:+++-------] Create domains \n"+
"......[33][100:++++++++++] foraging \n"+
".........[50:100][++++++++++] mission \n"+
".........[50][100:++++++++++] coordination methods \n"+
"............[33:100][++++++++++] noise \n"+
"............[33:100][++++++++++] repel \n"+
"............[34:100][++++++++++] aggression \n"+
"...[33][5:+---------] formation \n"+
"......[50:10][+---------] mission \n"+
"......[50][0:----------] coordination methods \n"+
".........[25:0][----------] ind. shift \n"+
".........[25:0][----------] soc. shift \n"+
".........[25:0][----------] dumb anchoring \n"+
".........[25:0][----------] collab. anchoring \n"+
"...[34][0:----------] corridor \n"+
"......[50:0][----------] mission \n"+
"......[50][0:----------] coordination methods \n"+
".........[33:0][----------] ... \n"+
".........[33:0][----------] ... \n"+
".........[34:0][----------] ... \n"+
"...[10][30:+++-------] write EI calculation \n"+
"......[30:100][++++++++++] behaviors for calculation \n"+
"......[70:0][----------] calculation \n"+
"...[10][0:----------] create test environment \n"+
"......[50:0][----------] test setup \n"+
"......[50:0][----------] test analyzer\n"+
"";	
	
	public ProgressTracker(){
		setTitle("ProgressTracker "+version);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setSize(new Dimension(600, 400));
		
		final JTextArea ta = new JTextArea();
		final JScrollPane tas = new JScrollPane(ta);	
		add(tas);
		final JButton btn = new JButton("Recalculate");
		add(btn);
		final JButton past = new JButton("Past");
		add(past);
		
		this.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
            	btn.setSize(new Dimension(120, 30));
            	btn.setLocation(getWidth() - 10 - btn.getWidth(), getHeight()-30-btn.getHeight());
            	past.setSize(new Dimension(120, 30));
            	past.setLocation(getWidth() - 10 - btn.getWidth() - 120 -10, getHeight()-30-btn.getHeight());
            	
            	tas.setSize(new Dimension(getWidth(), getHeight()-30-btn.getHeight() -10));
            	tas.setLocation(0, 0);

            	
            }
        });
		
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Caret c = ta.getCaret();
				ta.setText( analyzeText(ta.getText()) );
				ta.selectAll();
				ta.copy();
				ta.setCaret(c);
			}
		});
		past.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ta.paste();
				ta.selectAll();
			}
		});
	}
	
	
	
	class Progress{
		public int sum;
		public double progress;
		public double n1;
		public double n2;
		public int pos;
		public String l;
		public String tab;
		public int level;
		public Progress(int s, double p, double a1, double a2){
			sum = s;
			progress = p;
			n1 = a1;
			n2 = a2;
		}
	}
	String progressPrint(double p, int len, String done, String not){
		int di = (int)Math.round(len*p);
		String res = "";
		for(int i=0;i<di;i++){
			res+=done;
		}
		for(int i=0;i<len-di;i++){
			res+=not;
		}
		return res;
	}
	String correctLine(String line, double progress){
		int i = line.indexOf("]");
		if(line.substring(0,i).indexOf(":")>0){
			return line.substring(0,line.substring(0,i).indexOf(":"))+":"+(int)Math.round(progress*100)+"]["+progressPrint(progress, 10, "+","-")+"]"+""+line.substring(i+1)+" ";
		}
		return line.substring(0,i)+"]["+(int)Math.round(progress*100)+":"+progressPrint(progress, 10, "+","-")+"]"+""+line.substring(i+1)+" ";
	}
	String getTab(int ii, String t){
		String s = "";
		for(int i=0;i<ii;i++) s+=t;
		return s;
	}
	String analyzeText(String t){
		String[] lines = t.split("\n");
		String res = "";
		String tab="";
		String tabInk="     ";
		Stack<Progress> p = new Stack<Progress>();
		p.add(new Progress(0,0,0,0));
		Progress prog = p.pop();
		prog.level =0;
		for(String line : lines){
			int k= line.indexOf("[");
			if(k>=0){
				line = line.substring(k);
			}
			int i = line.indexOf("]");
			if( i<0 ) continue;
			int j = line.indexOf("[",i);
			if(j>0){
				int jj = line.indexOf("]",j);
				line = line.substring(0, j) + line.substring(jj+1);
			}
			String[] n = line.substring(1,i).split(":");
			if(n.length==1){
				//System.out.println(tab+""+n[0]);
				//res+=tab+correctLine(line, progress)+"\n";
				prog.pos = res.length();
				prog.l = line;
				prog.tab = tab;
				prog.n1 = Double.parseDouble(n[0])/100.0;
				prog.sum += Integer.parseInt(n[0]);
				p.push(prog);
				int lev = prog.level;
				tab+=tabInk;
				prog = new Progress(0, 0, 0, 0);
				prog.level = lev+1;
			}else{
				//System.out.println(tab+""+n[0]+" : "+n[1]);
				prog.n1 = Double.parseDouble(n[0])/100.0;
				prog.n2 = Double.parseDouble(n[1])/100.0;
				prog.sum += Integer.parseInt(n[0]);
				prog.progress += prog.n1* prog.n2;
				res+=getTab(prog.level,tabInk)+correctLine(line, prog.n2)/*+" --- "+prog.level*/+"\n";
				while(prog.sum >= 100 && p.empty()==false){
					Progress pprog = p.pop();
					tab=tab.substring(tab.length()-tabInk.length());
					pprog.n2 = prog.progress;
					pprog.progress += pprog.n1* pprog.n2;
					prog = pprog;
					res = res.substring(0,prog.pos)+getTab(prog.level,tabInk)+correctLine(prog.l, prog.n2)/*+" -- "+prog.level*/+"\n"+res.substring(prog.pos);
				}
			}
		}
		System.out.println(res);
		return res;
	}
	
	
	
	
	public static void main(String[] args) throws IOException{
		ProgressTracker tr = new ProgressTracker();
		tr.setVisible(true);
	}
}
